package controllerlayer;

import java.util.ArrayList;

import modellayer.Appointment;
import modellayer.KeyPerson;
import modellayer.Lead;
import modellayer.ZipCode;
import dblayer.LeadDB;

public class LeadCtrl {
	private LeadDB LeadDB;

	public LeadCtrl()
	{
		LeadDB = new LeadDB();
	
	}
	
	public boolean createLead(String leadCompanyName, String leadPersonName, String leadPhone, String leadMail, String leadWebsite, long addedDate) {
		boolean success = false;

		Lead l= new Lead(-1, leadCompanyName, leadPersonName, leadPhone, leadMail, leadWebsite, addedDate);

		try {
			if (LeadDB.insertLead(l) != -1) {
				success = true;
			}
		} catch (Exception e) {
			System.out
					.println("Failed to insert appointment into database! (LeadCtrl) Error: "
							+ e);
		}

		return success;
	}
	
	public ArrayList<Lead> getAllLeads() {
		return LeadDB.getLeads(null);
	}
	
	public Lead findLeadByName(String name)
	{
		int i = 0;
		boolean found = false;
		Lead result = null;
		ArrayList<Lead> array = getAllLeads();
		int arraySize = array.size();
		
		while (i<arraySize && !found)
		{
			if (array.get(i).getLeadPersonName().equals(name))
			{
				result = array.get(i);
				found = true;
			}
			i++;
		}
		
		return result;
	}
	
	 public boolean deleteLead(int leadId) throws Exception {
		 return LeadDB.deleteLead(leadId);
	 }
	
	
}
